package com.hub.videobrowsdownload

import android.content.Context
import android.os.Build
import android.view.ViewGroup
import android.webkit.CookieManager
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
import com.forest.download.helper.Default


@Composable
 fun BrowsMyWebView(
    tabWebViewModel: BrowsTabWebViewModel,
    updateWebview: (WebView) -> Unit
) {
    AndroidView(factory = {
        WebView(it).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )

            webViewClient = BrowsMyWebviewClien(this, tabWebViewModel)
            webChromeClient = BrowsMyWebChromeClient(tabWebViewModel)
            val webviewSettings = this.settings

            // cookie 相关
            val cookieManager0 = CookieManager.getInstance()
            var cookeAccept = 0
            if (cookeAccept == 0) {
                cookieManager0.setAcceptCookie(true);
                cookieManager0.setAcceptThirdPartyCookies(this, true);
            } else {
                if (cookeAccept == 1) {
                    cookieManager0.setAcceptCookie(true);
                } else {
                    cookieManager0.setAcceptCookie(false);
                }

                cookieManager0.setAcceptThirdPartyCookies(this, false);
            }
            if (Build.VERSION.SDK_INT >= 26) {
                try {
                    var isSafeBrowsing = true
                    webviewSettings.safeBrowsingEnabled = isSafeBrowsing
                } catch (unused_ex: Exception) {
                }
            }

            webviewSettings.setGeolocationEnabled(false)

//            var isDesktopMode = false
//            val userAgent:String
//            if (isDesktopMode) {
//                userAgent =
//                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
//            } else {
//                userAgent =
//                    "Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Mobile Safari/537.36"
//            }
            Default.userAgentString = webviewSettings.userAgentString
            try {
                val sp = BrowsApplication.context.getSharedPreferences("ua", Context.MODE_PRIVATE)
                sp.edit().putString("ua", webviewSettings.userAgentString).apply()
            } catch (e: Exception) {
            }
//            webviewSettings.userAgentString = userAgent

            webviewSettings.setJavaScriptEnabled(true);
            webviewSettings.setBuiltInZoomControls(true);
            webviewSettings.setMediaPlaybackRequiresUserGesture(true);
            webviewSettings.setOffscreenPreRaster(false);
            webviewSettings.setJavaScriptCanOpenWindowsAutomatically(false);
            webviewSettings.setAllowFileAccess(false);
            webviewSettings.setAllowContentAccess(true);
            webviewSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            webviewSettings.setBlockNetworkLoads(false);
            webviewSettings.setBlockNetworkImage(false);
            webviewSettings.setLoadsImagesAutomatically(true);
            webviewSettings.setDisplayZoomControls(false);
            webviewSettings.setUseWideViewPort(true);
            webviewSettings.setDomStorageEnabled(true);
            webviewSettings.setMixedContentMode(2);

            addJavascriptInterface(
                BrowsJavsscripShowVideo(
                    tabWebViewModel
                ), "VideoInfoJSI");
        }
    }, update = updateWebview)
}